/**
	DateUtil.js
	日期方法集合工具
	Creater:xzs Date:2016-01-15
	
	用法实例：
	无对象：$.setDate('2015-10-10','10') 返回'2015-10-20'
	有对象：$("#field8888").setDate('10'); 在原有天数上加10天并赋值
*/
(function($){
	
	var currentDate = new Date(); 
	
	/**************************有对象方法分界线**************************/
	
	/**
	* 两个日期比较并返回布尔值
	*/
	$.fn.compareDate = function(dateStr){
		return $.compareDate($(this).val(),dateStr);
	}
	
	/**
	* 两个日期比较并返回差值
	* type:y m w d h n s
	*/	
	$.fn.dateDiff = function(dateStr,type){
		return $.dateDiff($(this).val(),dateStr,type);
	}
	
	/**
	* 在日期上相加减天数并返回对应日期
	*/
	$.fn.setDate = function(num,type){
		$(this).val($.setDate($(this).val(),num,type));
	}
	
	/**************************无对象方法分界线**************************/
	
	/**
	* 获取当前完整年月日
	*/
	$.getFullDate = function(charStr){
		if(arguments.length==0){
			charStr = '-';
		}
		return $.getYear()+charStr+$.getMonth()+charStr+$.getDay();
	}	
	
	/**
	* 获取当前年份
	*/
	$.getYear = function(dateStr){
		return $.stringToDate(dateStr).getFullYear();
	}
	
	/**
	* 获取当前月份
	*/
	$.getMonth = function(dateStr){
		var month = $.stringToDate(dateStr).getMonth()+1;
		return month<10?"0"+month:month;
	}
	
	/**
	* 获取当前日期
	*/
	$.getDay = function(dateStr){
		var day = $.stringToDate(dateStr).getDay();
		return day<10?"0"+day:day;
	}
	
	/**
	* 获取当前日期是星期几
	*/
	$.getWeek = function(dateStr){ 
		var Week = ['7','1','2','3','4','5','6']; 
		return Week[$.stringToDate(dateStr).getDay()];
	}

	/**
	* 两个日期比较并返回布尔值
	*/
	$.compareDate = function(date1,date2){
		return $.stringToDate(date1).getTime() >= $.stringToDate(date2).getTime()?false:true;
	}

	/**
	* 在日期上相加减天数并返回对应日期
	*/
	$.setDate = function(dateStr,num,type){
		if(arguments.length==2){
			type = 'd';
		}
		var myDate = $.stringToDate(dateStr);
		
		switch(type){
			case 'd':myDate.setDate(myDate.getDate()+parseInt(num));break;
			case 'm':myDate.setMonth(myDate.getMonth()+parseInt(num));break;
			case 'y':myDate.setFullYear(myDate.getFullYear()+parseInt(num));break;
			default:myDate.setDate(myDate.getDate()+parseInt(num));
		}
		return $.formatDate(myDate);
	}

	/**
	* 在日期上相加减时间并返回对应日期
	*/
	$.setDateTime = function(dateStr,timeStr,num,type){
		if(arguments.length==3){
			type = 'h';
		}
		var myDate = $.stringToDate(dateStr,timeStr);
		switch(type){
			case 's' :myDate = new Date(myDate.getTime()+(1000*num)); break;
			case 'm' :myDate = new Date(myDate.getTime()+(60000*num)); break;
			case 'h' :myDate = new Date(myDate.getTime()+(3600000*num)); break;
			case 'd':myDate.setDate(myDate.getDate()+parseInt(num));break;
            case 'w' :myDate = new Date(myDate.getTime()+((86400000*7)*num));break;
			case 'm':myDate.setMonth(myDate.getMonth()+parseInt(num));break;
			case 'y':myDate.setFullYear(myDate.getFullYear()+parseInt(num));break;
			default:myDate.setDate(myDate.getDate()+parseInt(num));
		}
		return $.formatDate(myDate,'DateAndTime');
	}
	
	/**
	 * 计算两个日期的差值
	 */
	$.dateDiff = function(){ 
		var type = 'ms';
		if(arguments.length<4){
			date1 = $.stringToDate(arguments[0]); 
			date2 = $.stringToDate(arguments[1]);
			if(!$.isNull(arguments[2])){
				type = arguments[2];
			}
		}else if(arguments.length<6){
			date1 = $.stringToDate(arguments[0],arguments[1]); 
			date2 = $.stringToDate(arguments[2],arguments[3]); 
			if(!$.isNull(arguments[4])){
				type = arguments[4];
			}
		}
		
		switch (type) { 
			case 'ms':return parseInt(date2-date1); 
			case 's' :return (parseFloat((date2-date1)/1000)).toFixed(2);
			case 'm' :return (parseFloat((date2-date1)/60000)).toFixed(2); 
			case 'h' :return (parseFloat((date2-date1)/3600000)).toFixed(2); 
			case 'd' :return (parseFloat((date2-date1)/86400000)).toFixed(2); 
			case 'w' :return (parseFloat((date2-date1)/(86400000*7))).toFixed(2); 
			case 'm' :return (date2.getMonth()+1)+((date2.getFullYear()-date1.getFullYear())*12)-(date1.getMonth()+1); 
			case 'y' :return date2.getFullYear()-date1.getFullYear(); 
			default  :return parseInt(date2-date1);
		}
	}
	
	/**
	 * 将字符串转为日期类型
	 */
	$.stringToDate = function(dateStr,timeStr){
		var myDate = new Date(); 
		if ($.isValidDate(dateStr)){
			var arys = dateStr.split('-'); 
			if(arguments.length==2 && $.isValidTime(timeStr)){
				var arys2 = timeStr.split(':');
				myDate = new Date(arys[0],--arys[1],arys[2],arys2[0],arys2[1],arys2[2]); 
			}else{
				myDate = new Date(arys[0],--arys[1],arys[2]); 
			}
		}
		return myDate; 
	}
	
	/**
	 * 检验字符串是否为日期格式
	 */
	$.isValidDate = function(dateStr){
		var regex = /^(\d{4})-(\d{2})-(\d{2})$/;
		return regex.test(dateStr); 
	}

	/**
	 * 检验字符串是否为时间格式
	 */
	$.isValidTime = function(timeStr){
		var regex = /^(\d{1,2})(:)(\d{1,2})\2(\d{1,2})$/;
		return regex.test(timeStr); 
	}
	
	/**
	 * 检查对象是否为空
	 */	
	$.isNull = function(data){
		return (data == "" || data == undefined || data == null) ? true : false; 
	}
	
	/**
	* 日期格式化函数
	*/
	$.formatDate = function(date){
		var year = date.getFullYear();
		var month = date.getMonth()+1<10?"0"+(date.getMonth()+1):date.getMonth()+1;
		var day = date.getDate()<10?"0"+date.getDate():date.getDate();
		if(arguments.length==2){
			var hour = date.getHours()<10?"0"+date.getHours():date.getHours();
			var minute = date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes();
			var second = date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds();
			return [year+"-"+month+"-"+day,hour+":"+minute+":"+second];
		}else{
			return year+"-"+month+"-"+day;
		}
	}
})(jQuery)